package data 
{
	import flash.utils.getTimer;
	/**
	 * ...
	 * @author liwei
	 */
	public class LMS 
	{
		//达到分数
		public static var mastery_score:int=80;
		////////////////////
		public static var id:String;
		public static var location:int;
		public static var status:String;
		public static var score:int;
		public static var time:String;
		public static var evaluation:int;
		public static var scormData:String;
		public static var aiccData:String;
		public function LMS() 
		{
			
		}
		public static function decodeScorm(value:String):Boolean {
			//tbc_progress=0,lesson_location,status,score,time,@SCORM@id_status_score_test,id_status_score_test,
			if (value == null || value == "") return false;
			var arr:Array = value.split(",@SCORM@");
			var setstr:String = arr[0];
			var setarr:Array = setstr.split(",");
			location = setarr[1];
			status = setarr[2];
			score = setarr[3];
			time = setarr[4];
			var lessonstr:String = arr[1];
			var lessonarr:Array = lessonstr.split(",");
			var len:int = lessonarr.length;
			for (var i:int = 0; i < len; i++) {
				var part:String = lessonarr[i];
				var partarr:Array = part.split("_");
				if (partarr.length > 3) {
					var lid:int = partarr[0];
					var lstatus:String = partarr[1];
					var lscore:int = partarr[2];
					var ltest:String = partarr[3];
					var lesson:Lesson = Chapter.getLessonById(lid);
					if (lesson != null) {
						lesson.status = lstatus;
						lesson.score = lscore;
						lesson.test = ltest;
					}
				}
			}
			return true;
		}
		public static function encode():void {
			score = 0;
			var lessons:Array = Chapter.getLessons();
			var len:int = lessons.length;
			status = "incomplete";
			var completeCount:int = 0;
			var lesson:Lesson;
			var i:int;
			var lessonAicc:String = "";
			var lessonScorm:String = "";
			for (i = 0; i < len; i++) {
				lesson = lessons[i];
				score += lesson.score;
				lessonAicc += lesson.getAiccData();
				lessonScorm += lesson.getScormData()+",";
				if (lesson.hasComplete()) {
					completeCount++;
				}
			}
			if (completeCount == len) {
				status = "completed";
			}
			evaluation = Math.floor(completeCount / len * 100);
			time = getTime();
			lesson = Chapter.getLessonByFlag(State.chapter, State.lesson);
			location = lesson.id;
			aiccData = "";
			scormData = "";
			var info:String = "";
			aiccData = "[Core]\r\n";
			aiccData += "Lesson_Status="+status+"\r\n";
			aiccData += "Lesson_Location="+location+"\r\n";
			aiccData += "score="+score+"\r\n";
			aiccData += "Time="+time+"\r\n";
			aiccData += "[Core_Lesson]\r\n";
			aiccData += "[Comments]\r\n";
			aiccData += "[Objectives_Status]\r\n";
			aiccData += lessonAicc;
			
			scormData="tbc_progress="+evaluation+","+location+","+status+","+score+","+time+","+"@SCORM@";
			scormData += lessonScorm;
		}
		private static function getTime():String {
			var s:int = (getTimer() - State.startTime)/1000;
			var HH:int = int(s/3600);
			var MM:int = int((s-HH*3600)/60);
			var SS:int = int(s-HH*3600-MM*60);
			return (fill0(HH.toString(),-1)+":"+fill0(MM.toString(),-1)+":"+fill0(SS.toString(),-1));
		}
		public static function fill0(x:String, n:int):String
		{
			var left:Boolean = n < 0;
			n = n < 0? -n:n;
			var s:String = "";
			for (var i:int = 0; i < (n - x.length);i++ ) {
				s += '0';
			}
			if (left) {
				return s + x;
			}else {
				return x + s;
			}
		}
	}

}